<template>
    <div>
      <el-card style="width: 600px; margin:30px 0 0 250px; background-color:floralwhite">
        <el-form label-width="80px" size="medium ">
          <el-upload
            class="avatar-uploader"
            action="http://8.138.58.18:7777/file/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
          <img v-if="form.avatarUrl" :src="form.avatarUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>

          <el-form-item label="姓名:">
              <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="用户名:">
              <el-input disabled v-model="form.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电话:">
              <el-input v-model="form.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱:">
              <el-input v-model="form.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="地址:">
              <el-input type="textarea" v-model="form.address" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
              <el-button type="primary" @click="save">确定</el-button>
          </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>

export default {
  name: 'Information',
  data(){
    return{
      form: {},
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  created(){
    this.getUser().then(res => {
      console.log(res)
      this.form = res
    })
  },
  methods:{
    async getUser() {
      return (await this.request.get("/user/" + this.user.id)).data
    },
    save(){
        console.log('111');
        this.request.put('/user/edit',this.form).then(res => {
            console.log('res:',res);
        if (res) {
            this.$message.success("用户信息保存成功")
            //触发父级更新User的方法
            this.$emit("refreshUser")
            //更新浏览器存储的用户信息
            this.getUser().then(res =>{
              res.token = JSON.parse(localStorage.getItem("user")).token
              localStorage.setItem("user",JSON.stringify(res))
            })
        } else {
          this.$message.error("用户信息保存失败")
        }
        })
    },
    handleAvatarSuccess(res) {
      this.form.avatarUrl = res
    },
  }
};
</script>

<style>
.avatar-uploader {
  text-align: center;
  padding-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  border-radius: 50%;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}
.avatar {
  width: 138px;
  height: 138px;
  display: block;
  border-radius: 50%;
}
</style>